//
//  NSString+time.m
//  RM_IM
//
//  Created by zhangzhenyun on 2018/7/10.
//  Copyright © 2018年 user1. All rights reserved.
//

#import "NSString+time.h"

@implementation NSString (time)
+(NSString *)transTimeintervalToStr:(NSTimeInterval)timeInterval sinceFrom:(SinceFromType)when {
    NSTimeInterval nowInterval;
    if (when == SinceFromTypeNow) {
        nowInterval = [[NSDate date] timeIntervalSinceNow];
    } else {
        nowInterval = [[NSDate date] timeIntervalSince1970];
    }
    NSTimeInterval distance = nowInterval - timeInterval;
    double oneDay = 24 * 60 * 60;
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSDate * publishDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    
    if (distance < oneDay) {
        [formatter setDateFormat:@"HH:mm"];
        NSLog(@"今天");
    } else if (distance < 365 * oneDay) {
        [formatter setDateFormat:@"MM月dd日"];
        NSLog(@"几年");
    } else {
        [formatter setDateFormat:@"yyyy年MM月dd日"];
        NSLog(@"具体年份");
    }
    NSString *dateStr = [formatter stringFromDate:publishDate];
    return dateStr;
}
+ (NSString *)timeFormatDescriptionFromtime:(NSString *)time {
    NSCalendar *currentCal = [NSCalendar currentCalendar];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSDate *currentDate = [NSDate date];
    NSDate *theDate = [formatter dateFromString:time];
    
    NSInteger year = [currentCal component:NSCalendarUnitYear fromDate:currentDate];
    NSInteger theYear = [currentCal component:NSCalendarUnitYear fromDate:theDate];
    
    NSInteger month = [currentCal component:NSCalendarUnitMonth fromDate:currentDate];
    NSInteger theMonth = [currentCal component:NSCalendarUnitMonth fromDate:theDate];
    
    NSInteger day = [currentCal component:NSCalendarUnitDay fromDate:currentDate];
    NSInteger theDay = [currentCal component:NSCalendarUnitDay fromDate:theDate];
    
    NSString *timeForDes = @"";
    
    if (year - theYear > 0) {//年月日
        timeForDes =  [NSString stringWithFormat:@"%zi年%zi月%zi日",theYear,theMonth,theDay];
    } else {
        if (month - theMonth > 0 || day - theDay > 0) {//月日
            timeForDes = [NSString stringWithFormat:@"%zi月%zi日",theMonth,theDay];
        } else {//时分
            timeForDes = [time substringWithRange:NSMakeRange(11, 5)];
        }
    }
    return timeForDes;
}
@end
